package com.kpmg.datalake.reportanalysis.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.kpmg.datalake.common.controller.BaseController;
import com.kpmg.datalake.common.model.DimInfo;
import com.kpmg.datalake.common.model.ReportDate;
import com.kpmg.datalake.common.privilege.ValidateDataAuth;
import com.kpmg.datalake.common.service.BaseInfoService;
import com.kpmg.datalake.common.service.IPrjRptSqlMpngService;
import com.kpmg.datalake.common.utils.ContextHolderUtils;
import com.kpmg.datalake.common.vo.BasePageQueryVO;
import com.kpmg.datalake.common.vo.BaseQueryVO;
import com.kpmg.datalake.common.vo.GeneralHeaderVO;
import com.kpmg.datalake.common.vo.ServerResponse;
import com.kpmg.datalake.operatelog.annotation.OptLog;
import com.kpmg.datalake.operatelog.enums.LogTypeEnum;
import com.kpmg.datalake.reportanalysis.service.ReportService;
import com.kpmg.datalake.reportanalysis.vo.MultiDimRequestVO;
import com.kpmg.datalake.reportanalysis.vo.PublicHeadVO;
import com.kpmg.datalake.reportanalysis.vo.ReportTemplateVO;
import com.kpmg.datalake.reportanalysis.vo.TableHeaderVO;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 报表相关操作控制器
 *
 * @author Tony
 */
@RestController
@RequestMapping("/report")
public class ReportController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(ReportController.class);
  @Resource
  private ReportService reportService;
  @Resource
  private BaseInfoService baseInfoService;


  @Resource
  private IPrjRptSqlMpngService prjRptSqlMpngService;


  /**
   * 初始化客户名称
   */
  @ApiOperation(value = "initCorp", notes = "初始化客户名称", produces = "application/json")
  @PostMapping("/initCorp")
  public ServerResponse<PageInfo<DimInfo>> initCorp(@RequestBody BasePageQueryVO vo) {
    try {
      // 查询客户维度信息
      PageInfo<DimInfo> pageInfo = baseInfoService
          .initCorp(vo.getPageNum(), vo.getPageSize(), vo.getProjectId(), vo.getQueryParam());
      // 返回信息
      return super.doSuccess(pageInfo);
    } catch (Exception e) {
      logger.error("初始化客户名称时异常:{}", e.getMessage(), e);
      return super.doError(e);
    }
  }

  /**
   * 初始化报表日期范围
   */
  @ApiOperation(value = "initReportDate", notes = "初始化报表日期范围", produces = "application/json")
  @PostMapping("/initReportDate")
  public ServerResponse<ArrayList<ReportDate>> initReportDate(
      @RequestBody BaseQueryVO baseQueryVO) {
    try {
      // 查询客户维度信息
      List<ReportDate> reportDate = baseInfoService.initDate(baseQueryVO.getProjectId());

      // 返回信息
      return ServerResponse.createBySuccess("初始化报表日期范围成功！", new ArrayList<>(reportDate));
    } catch (Exception e) {
      logger.error("初始化报表日期范围时异常:{}", e.getMessage(), e);
      return ServerResponse.createByErrorMessage("初始化报表日期范围失败，异常:" + e.getMessage());
    }
  }


  /**
   * 获取用户历史模板
   */
  @ApiOperation(value = "getUserRptTemplates", notes = "获取用户历史模板", produces = "application/json")
  @PostMapping("/getUserRptTemplates")
  public ServerResponse<PageInfo<Map<String, Object>>> getUserRptTemplates(
      @RequestBody BasePageQueryVO vo) {
    return reportService
        .getUserRptTemplates(ContextHolderUtils.getLoginUser().getUsrId(), vo.getProjectId(),
            vo.getQueryParam(), vo.getPageNum(), vo.getPageSize());
  }

  /**
   * 保存报表筛选条件
   */
  @ApiOperation(value = "saveSearchList", notes = "保存报表筛选条件", produces = "application/json")
  @PostMapping("/saveSearchList")
  @OptLog(logType = LogTypeEnum.OPERATION, module = "reportAnalysis", description = "保存表单")
  public ServerResponse<String> saveSearchList(@RequestBody ReportTemplateVO vo) {
    return reportService.saveCondition(vo.getProjectId(), vo);
  }

  /**
   * 删除用户历史模板
   */
  @ApiOperation(value = "userRptTemplate", notes = "删除用户历史模板", produces = "application/json")
  @GetMapping("/userRptTemplate")
  public ServerResponse<String> userRptTemplate(@RequestParam("projectId") String projectId,
      @RequestParam("smyId") String smyId) {
    return reportService.delUserTemplate(projectId, smyId);
  }

  /**
   * 获取动态表头
   */
  @PostMapping("/getMultiHeader")
  public ServerResponse<ArrayList<TableHeaderVO>> getMultiHeader(
      @RequestBody BaseQueryVO baseQueryVO) {
    return ServerResponse.createBySuccess(reportService.getMultiHeader(baseQueryVO.getProjectId()));
  }

  /**
   * 获取动态列数据
   */
  @PostMapping("/getMultiData")
  @OptLog(logType = LogTypeEnum.OPERATION, module = "reportAnalyze", description = "多维分析查询")
  @ValidateDataAuth(key = "#multiDimRequestVO.getProjectId()")
  public ServerResponse<Page<Map<String, Object>>> getMultiData(
      @RequestBody MultiDimRequestVO multiDimRequestVO) {
    Page<Map<String, Object>> multiDimReportData = reportService
        .getMultiDimReportData(multiDimRequestVO);

    return ServerResponse.createBySuccess(multiDimReportData);
  }

  /**
   * 前端导出
   *
   * @return ServerResponse
   */
  @PostMapping("/getExportData")
  @OptLog(logType = LogTypeEnum.OPERATION, module = "reportAnalyze", description = "多维导出excel")
  public ServerResponse<String> getExportData() {
    return ServerResponse.createBySuccess();
  }

  /**
   * 初始化科目
   */
  @ApiOperation(value = "getSubject", notes = "初始化科目", produces = "application/json")
  @PostMapping("/getSubject")
  public ServerResponse<PageInfo<DimInfo>> getSubject(@RequestBody BasePageQueryVO vo) {
    // 查询科目维度信息
    return reportService
        .initSbj(vo.getPageNum(), vo.getPageSize(), vo.getProjectId(), vo.getQueryParam());
  }

  /**
   * 初始化辅助核算项目
   */
  @PostMapping("/getAuxDim")
  public ServerResponse<PageInfo<DimInfo>> queryOtherDim(@RequestBody BasePageQueryVO vo) {
    // 查询辅助核算维度信息
    return reportService
        .initAuxDim(vo.getPageNum(), vo.getPageSize(), vo.getProjectId(), vo.getQueryParam(),
            vo.getTableName());
  }

  /**
   * VBA获取动态表头
   */
  @PostMapping("/getPublicHeader")
  public ServerResponse<ArrayList<GeneralHeaderVO>> getPublicHeader(
      @RequestBody PublicHeadVO publicHeadVO) {
    ArrayList<GeneralHeaderVO> headList = (ArrayList<GeneralHeaderVO>) prjRptSqlMpngService
        .selectGeneralHeader(publicHeadVO.getProjectId(), publicHeadVO.getRptSqlId(),
            ContextHolderUtils.getLocale().toString());
    return ServerResponse.createBySuccess(headList);
  }

}
